package com.sq.News.controller;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.google.gson.Gson;
import com.sq.News.pojo.NewsInfo;
import com.sq.News.pojo.RequestInfo;
import com.sq.News.pojo.UserInfo;
import com.sq.News.service.NewsService;
import com.sq.News.service.UserService;

@Controller
public class UserController {
	private static boolean isLogined = false;
	@Autowired
	private NewsService newsService;
	@Autowired
	private UserService userService;

	@Autowired
	private RequestInfo requestInfo;

	@RequestMapping("/backManagent")
	public ModelAndView backManagent(String user, String code) {

		ModelAndView mv = null;
		if (isLogined) {
			ArrayList<NewsInfo> newsInfos = newsService.getAllNews();
			mv = new ModelAndView("backManagent");
			mv.addObject("NewsInfos", newsInfos);
			mv.addObject("user", user);
			if (code != null) {
				mv.addObject("code", code);
			}
			return mv;
		} else {
			mv = new ModelAndView("redirect:/login.html");
			return mv;
		}

	}

	@RequestMapping("/delayNews")
	public ModelAndView getUserList() {
		ModelAndView mv = null;
		if (isLogined) {
			try {
				mv = new ModelAndView("delayNews");
				ArrayList<NewsInfo> newsInfos = newsService.getAllNews();
				mv.addObject("NewsInfos", newsInfos);
			} catch (Exception e) {
				e.printStackTrace();
			}
		} else {
			mv = new ModelAndView("redirect:/login.html");
		}
		return mv;
	}

	// 点击退出登录按钮后，将登录状态置为假
	@RequestMapping("/exit")
	public String exitLogin() {
		isLogined = false;
		return "redirect:login.html";
	}

	// 进入发布新闻界面
	@RequestMapping(value = "/add")
	public String returAaddNews() {
		if (isLogined) {
			return "addNews";
		} else {
			return "redirect:login.html";
		}
	}

	// 进入更新新闻界面
	@RequestMapping("/update")
	public String updateNews() {
		if (isLogined) {
			return "updateNews";
		} else {
			return "redirect:login.html";
		}
	}

	// 进入管理员登录界面
	@RequestMapping(value = "/login")
	public String addNews() {
		return "html/login";
	}

	@RequestMapping(value = "/loginCheck")
	@ResponseBody
	public String loginCheck(UserInfo user, HttpServletRequest httpServletRequest) {
		UserInfo userInfo = userService.getUser(user.getName());
		if (userInfo == null) {
			requestInfo.setState(101);
			requestInfo.setMessage("login fail");
			return new Gson().toJson(requestInfo);
		}
		if (userInfo != null && user.getPwd().equals(userInfo.getPwd())) {
			requestInfo.setState(200);
			requestInfo.setMessage("login success");
			isLogined = true;
			return new Gson().toJson(requestInfo);
		} else {
			requestInfo.setState(101);
			requestInfo.setMessage("login fail");
			return new Gson().toJson(requestInfo);
		}
	}

	// 进入删除新闻界面
	public String deleteNews() {
		if (isLogined) {
			return "deleteNews";
		} else {
			return "redirect:login.html";
		}
	}
}
